package cn.itcast.mq.service.impl;

import cn.itcast.mq.service.IMessageProvider;
import java.util.UUID;
import javax.annotation.Resource;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

/**
 * @program: mq-demo
 * @ClassName MessageProviderImpl
 * @description:
 * @author: zsn
 * @Version 1.0
 **/
@Slf4j
@EnableBinding(Source.class)
public class MessageProviderImpl implements IMessageProvider {

    // 消息的发送管道
    @Resource
    private MessageChannel output;

    @Override
    public String send() {
        String serial = UUID.randomUUID().toString();
        // 创建并发送消息
        Message<String> message = MessageBuilder
                .withPayload(serial)
                .setHeader("x-delay", 5000)
                .build();
        this.output.send(message);
        log.info("发送成功: " + serial);
//        System.out.println("***serial: " + serial);
        return serial;
    }
}